<template>
  <div>
    <el-table :data="form" style="width: 100%">
      <el-table-column prop="name" label="姓名" width="180"> </el-table-column>
      <el-table-column prop="position" label="职位" width="180">
      </el-table-column>
      <el-table-column prop="basicSalary" label="基础工资"> </el-table-column>
      <el-table-column prop="bonus" label="奖金"> </el-table-column>
      <el-table-column prop="taxDeduction" label="税收"> </el-table-column>
      <el-table-column prop="netSalary" label="实发工资"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="200">
        <template slot-scope="scope">
          <el-button
            @click="handleClickEdit(scope.row)"
            type="primary"
            size="small"
            >修改</el-button
          >
          <el-button
            @click="handleClickdelete(scope.row)"
            type="danger"
            size="small"
            v-if="scope.row.state === 0"
            >生成工资单</el-button
          >
          <el-button
            type="info"
            size="small"
            v-if="scope.row.state === 1"
            disabled
            >工资单已生成</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹出修改框 -->
    <el-dialog
      title="薪水修改"
      :visible.sync="dialogFormVisible"
      close-on-click-modal="true"
    >
      <el-form :model="salar" :rules="rules">
        <el-form-item label="姓名" :label-width="formLabelWidth">
          <el-input v-model="salar.name" disabled></el-input>
        </el-form-item>
        <el-form-item label="职位" :label-width="formLabelWidth">
          <el-input v-model="salar.position" disabled></el-input>
        </el-form-item>
        <el-form-item
          label="基础工资"
          prop="basicSalary"
          :label-width="formLabelWidth"
        >
          <el-input v-model="salar.basicSalary" @input="handleInput"></el-input>
        </el-form-item>
        <el-form-item label="奖金" prop="bonus" :label-width="formLabelWidth">
          <el-input v-model="salar.bonus" @input="handleInput"></el-input>
        </el-form-item>
        <el-form-item
          label="税收"
          prop="taxDeduction"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="salar.taxDeduction"
            @input="handleInput"
          ></el-input>
        </el-form-item>

        <el-form-item
          label="实发工资"
          prop="netSalary"
          :label-width="formLabelWidth"
        >
          <el-input v-model="salar.netSalary" disabled></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addClose">取 消</el-button>
        <el-button type="primary" @click="edit(salar)">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getAllSalar, updeteSalary, createSalary } from '@/api/salary';
import { getToken } from '@/utils/auth';
export default {
  created() {
    // 获取所有人薪水
    this.getSalarList();
  },
  methods: {
    getSalarList() {
      getAllSalar().then((resp) => {
        this.form = resp.data;
      });
    },
    handleClickEdit(row) {
      this.dialogFormVisible = true;
      this.salar = row;
    },
    // 生成工资单
    handleClickdelete(row) {
      this.salar = row;
      createSalary(this.salar).then((resp) => {
        this.getSalarList();
        this.$message({
          message: resp.data,
          type: 'success',
        });
      });
    },
    handleInput(value) {
      this.salar.netSalary =
        parseInt(this.salar.basicSalary) +
        parseInt(this.salar.bonus) -
        parseInt(this.salar.taxDeduction);
    },
    addClose() {
      this.dialogFormVisible = false;
      this.getSalarList();
    },
    edit(salar) {
      updeteSalary(this.salar).then((resp) => {
        this.$message({
          message: resp.data,
          type: 'success',
        });
      });
      this.dialogFormVisible = false;
    },
  },
  data() {
    return {
      form: [],
      dialogFormVisible: false,
      formLabelWidth: '120px',
      salar: [],
      rules: {
        basicSalary: [
          { required: true, message: '基础工资不能为负数' },
          {
            //验证为非负数
            validator: (rule, value, callback) => {
              if (value < 0) {
                callback(new Error('基础工资不能为负数'));
              } else {
                callback();
              }
            },
          },
        ],
        bonus: [
          { required: true, message: '奖金不能为负数' },
          {
            //验证为非负数
            validator: (rule, value, callback) => {
              if (value < 0) {
                callback(new Error('奖金不能为负数'));
              } else {
                callback();
              }
            },
          },
        ],
        taxDeduction: [
          { required: true, message: '税额不能为负数' },
          {
            //验证为非负数
            validator: (rule, value, callback) => {
              if (value < 0) {
                callback(new Error('税额不能为负数'));
              } else {
                callback();
              }
            },
          },
        ],
        netSalary: [
          { required: true, message: '薪水不能为负数' },
          {
            //验证为非负数
            validator: (rule, value, callback) => {
              if (value < 0) {
                callback(new Error('薪水不能为负数'));
              } else {
                callback();
              }
            },
          },
        ],
      },
    };
  },
};
</script>
